import xlrd,xlwt
import sys
import os
from xlutils.copy import copy
p=os.path.dirname(__file__)
parent_path = os.path.dirname(p)
sys.path.append(parent_path)
from ulits.order_tool import Excel
file_path_sell=parent_path+'/data/buy_order.xls'
file_path_over=parent_path+'/data/over_order.xls'
class Buyer(object):
    def __init__(self,ID):
        self.ID=ID
        self.path_sell=file_path_sell
        self.path_over=file_path_over
    def show_un_f(self):
        find_rd=xlrd.open_workbook(self.path_sell)
        sheet=find_rd.sheet_by_name('买家订单')
        for i in range(sheet.nrows):
            values=sheet.row_values(i)
            if values[5]==self.ID:
                for j in values:
                    print(j,end=' '*6)
                print('')
    def cancel_order(self):
        excel = Excel(self.path_sell)
        excel.find_data('买家订单')
        excel.filter_buyer(self.ID)    
        cancel=[]
        no_cancel=[]
        yes_order = []
        for i in excel.use_a:
            if i[-3] == '已付款待发货':
                cancel.append(i)
            else:
                no_cancel.append(i)
        if cancel == []:
            print('没有可取消的订单')
        else:
            while True:
                if cancel == []:
                    print('没有可取消的订单')
                    break
                else:
                    for i,j in enumerate(cancel,1):
                        print('编号:{}\t'.format(i),j)
                    number1 = input('请输入要取消的编号(返回请按Q/q):')
                    if number1.upper() == 'Q':
                        break
                    elif number1.isdigit():
                        number1=int(number1)
                        if number1 <= len(cancel):
                            cancel[number1-1][-3] = '买家已取消该订单'
                            no_cancel.append(cancel[number1-1])
                            del cancel[number1-1]
                            print('取消申请已发出，等待商家处理')
                        else:
                            print('输入错误')
            yes_order = no_cancel + cancel + excel.nouse_a
            excel.correct_data(yes_order)
        excel.use_a.clear()
        excel.nouse_a.clear()
        excel.a.clear()
    def receive_order(self):
        excel = Excel(self.path_sell)
        excel.find_data('买家订单')
        excel.filter_buyer(self.ID)
        take_goods = []
        notake_goods = []
        c_transaction = []
        end = []
        for i in excel.use_a:
            if i[-3] == '已发货':
                take_goods.append(i)
            else:
                notake_goods.append(i)
        while True:
            if  take_goods == []:
                print('没有可收货商品')
                break
            else:
                for i,j in enumerate(take_goods,1):
                    print('编号{}'.format(i),j)
                number2=input('请输入要收货的编号(返回请按Q/q):')
                if number2.upper() == 'Q':
                    end = take_goods + notake_goods + excel.nouse_a
                    break
                elif number2.isdigit():
                    number2 = int(number2)
                    if number2 <= len(take_goods):
                        take_goods[number2-1][-3]='已收货'
                        c_transaction.append(take_goods[number2-1])
                        del take_goods[number2-1]
                        print('收货成功，是否立即评价 1.立即评价  2.以后再评价')
                        review = input('请选择：')
                        if review == '1':
                            evaluate_v = input('请输入评价内容:')
                            c_transaction[-1].append(evaluate_v)
                        else:
                            evaluate_v = '买家未评价'
                            c_transaction[-1].append(evaluate_v)
                        excel1 = Excel(self.path_over)
                        excel1.add_date(c_transaction[-1])
                    else:
                        print('输入错误')
                else:
                    print('输入错误')
        excel0 = Excel(self.path_sell)
        excel0.get_head('买家订单')
        excel0.delete_all_data('买家订单',excel0.slist)
        excel0.correct_data(end)
        excel.use_a.clear()
        excel.nouse_a.clear()
        excel.a.clear()
    def show_f(self):
        excel1 = Excel(self.path_over)
        excel1.find_data('订单记录')
        excel1.filter_buyer(self.ID)
        if excel1.use_a == []:
            print('本平台暂无您的交易记录')
        else:
            for i in excel1.use_a:
                print(i)
    def reviwe(self):
        excel1 = Excel(self.path_over)
        excel1.find_data('订单记录')
        excel1.filter_buyer(self.ID)
        for i,j in enumerate(excel1.use_a,1):
            print('编号{}\t'.format(i),j)
        number3 = input('请选择你要修改评价的商品编号：')
        if number3.isdigit():
            number3 = int(number3)
            if number3 <= len(excel1.a):
                correct_v = input('请输入修改后的评价内容:')
                excel1.a[number3-1][-1] = correct_v
                excel1.correct_data(excel1.use_a + excel1.nouse_a)
                print('评价修改成功')
            else:
                print('输入错误')
        else:
            print('输入错误')
        excel1.a.clear()
    def vive(self):
        print('''
            1.查看订单
            2.取消订单
            3.确认收货
            4.查看评价
            5.修改评价
            6.退出''')
